package com.google.code.coma.space;

import com.google.code.coma.domain.Part;
import com.google.code.coma.domain.Type;
import org.mozartspaces.capi3.Capi3Exception;
import org.mozartspaces.capi3.javanative.operation.NativeEntry;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 */
public class TypeAmountPartSelectorCallback implements SelectorCallback {

    private int amount;
    private Type type;

    public TypeAmountPartSelectorCallback(int amount, Type type) {
        this.amount = amount;
        this.type = type;
    }

    @Override
    public List<NativeEntry> doInTransaction(Collection<NativeEntry> coordinatorCollection, CallContext callContext) throws Capi3Exception {
        List<NativeEntry> resultList = new ArrayList<NativeEntry>();
		for (NativeEntry e : coordinatorCollection) {
			if (e.getData() instanceof Part) {
                Part part = (Part)e.getData();

                if (part.getType().equals(type)) {
                    resultList.add(e);
                }
            }

            if (resultList.size() == amount) {
                return resultList;
            }
		}
		return resultList;
    }
}
